/*
 * ModelReconstructor.h
 *
 *  Created on: Apr 14, 2009
 *      Author: wrice
 */

#ifndef MODELRECONSTRUCTOR_H_
#define MODELRECONSTRUCTOR_H_
#include "MyVideoTrace.h"
namespace MyVideoTrace {


class ModelReconstructor {
public:
	ModelReconstructor();

public: // observers
	size_t sizeFeaturePoints() const;
	bool hasFeaturePoint(FrameNumber whichFrame, Point2D point) const;
	bool hasFeaturePoint( FeaturePoint * featurePoint ) const;
	bool hasFeatureLine(FeaturePoint * lineFrom, FeaturePoint * lineTo);
	FeaturePoint * findFeaturePoint(FrameNumber whichFrame, Point2D point) const;
	FeaturePoint * findNearFeaturePoint(FrameNumber whichFrame, Point2D point, float distance) const;
	Point2D getProjectionPoint( FrameNumber frameNumber, FeaturePoint * featurePoint );

public: // for input
	FeaturePoint * addPoint2D(FrameNumber whichFrame, Point2D point);

	void addLine3D( FeaturePoint * lineFrom, FeaturePoint * lineTo );

	void moveFeaturePoint(FeaturePoint * fpoint, FrameNumber whichFrame, Point2D moveTo);

	void removeFeaturePoint( FeaturePoint * fpoint );

	void importVoodoo( const VoodooLoader * voodooLoader );
	void saveReconstructedModelOnFile( string filename );

public: // for output
	typedef list< Point2D > PointsOnFrame;
	typedef PointsOnFrame::const_iterator PointsOnFrameIterator;
	typedef list< Line2D > LinesOnFrame;
	typedef LinesOnFrame::const_iterator LinesOnFrameIterator;

	PointsOnFrame getVoodooFeaturePointsOnOneFrame( FrameNumber frameNumber );
	PointsOnFrame getUserAddedFeaturePointsOnOneFrame( FrameNumber frameNumber );
	PointsOnFrame getPotentialPointsOnOneFrame( FrameNumber frameNumber );
	LinesOnFrame getLinesOnOneFrame( FrameNumber frameNumber );

private:
	struct Impl;
	typedef set< FeaturePointPtr, FeaturePoint::Comparator > FeaturePoints;
	typedef FeaturePoints::const_iterator FeaturePointsIterator;
	typedef FeaturePoints::iterator FeaturePointsModifiableIterator;

	typedef set< FeatureLine, FeatureLine::Comparator > FeatureLines;
	typedef FeatureLines::const_iterator FeatureLinesIterator;
	typedef FeatureLines::iterator FeatureLinesModifiableIterator;

	FeaturePoints featurePoints_;
	FeatureLines featureLines_;
	const VoodooLoader * voodooLoader_;

	FeaturePointsModifiableIterator findFeaturePointForModify(FrameNumber frameNumber, Point2D point);

	int generateNewFeaturePointID();
	void adjust3DCoordination( FeaturePoint * featurePoint );

	typedef set< FeaturePoint * > FeaturePointsForTriangle;
	typedef FeaturePointsForTriangle::const_iterator FeaturePointsForTriangleIterator;
	FeaturePointsForTriangle findClosingFeaturePointsForTriangle( FeatureLine line );

private:
	static CvMat getInvertMatrix( CvMat M );
	static Vector3f getProjectionPosition( VoodooCameraInfo camera, Vector3f posIn3D );
	static Vector3f getRevertedPosition( VoodooCameraInfo camera, Vector3f posIn2D );
	static Point2D getProjectionPoint( VoodooCameraInfo camera, Vector3f point3D );


public: virtual ~ModelReconstructor() {}
};

}

#endif /* MODELRECONSTRUCTOR_H_ */
